package com.galudisu.handsonic.modules.system.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.galudisu.handsonic.base.AudiTableEntity;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Barudisshu
 */
@Entity
@Table(name = "t_sys_role")
public class Role extends AudiTableEntity {
    private static final long serialVersionUID = 5385401987199430477L;

    @Basic
    @Column(name = "name", nullable = false, length = 100)
    private String name;
    /**
     * 角色编码，用于生成权限框架的惟一标识权限
     */
    @Basic
    @Column(name = "identity", nullable = false,unique = true, length = 100)
    private String identity;
    @Basic
    @Column(name = "available", nullable = true)
    private Boolean available;

    //------------------------------------------------------------


    @JsonIgnore
    @ManyToOne(cascade = {CascadeType.ALL})
    @JoinColumn(name = "parent_id")
    private Role parentId;

    @JsonIgnore
    @OneToMany(mappedBy = "parentId")
    private Set<Role> childs = new HashSet<>();


    @JsonIgnore
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "t_sys_user_role",
            joinColumns = {@JoinColumn(name = "role_id", nullable = false, updatable = false)},
            inverseJoinColumns = {@JoinColumn(name = "user_id", nullable = false, updatable = false)})
    private Set<User> users = new HashSet<>();

    @JsonIgnore
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "t_sys_role_resource",
            joinColumns = {@JoinColumn(name = "role_id", nullable = false, updatable = false)},
            inverseJoinColumns = {@JoinColumn(name = "resource_id", nullable = false, updatable = false)})
    private Set<Resource> resources = new HashSet<>();


    public Role(){}

    public Role(String name,String identity){
        this.name = name;
        this.identity = identity;
    }

    // getter setter

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIdentity() {
        return identity;
    }

    public void setIdentity(String identity) {
        this.identity = identity;
    }

    public Boolean getAvailable() {
        return available;
    }

    public void setAvailable(Boolean available) {
        this.available = available;
    }

    public Role getParentId() {
        return parentId;
    }

    public void setParentId(Role parentId) {
        this.parentId = parentId;
    }

    public Set<Role> getChilds() {
        return childs;
    }

    public void setChilds(Set<Role> childs) {
        this.childs = childs;
    }

    public Set<User> getUsers() {
        return users;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }

    public Set<Resource> getResources() {
        return resources;
    }

    public void setResources(Set<Resource> resources) {
        this.resources = resources;
    }
}
